package com.tencent.elife.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

/**
 * @Description:
 * 设置界面常用的类listView的控件，这里实现了一个抽象的基类，具体使用时须继承并实现本类抽象方法。
 * @date 2012-6-1
 * @author zijianlu
 */
public abstract class AbstractSettingListView extends LinearLayout implements
    OnClickListener {

    private Context mContext;
    protected int mSelectorUp = 0;
    protected int mSelectorMid = 0;
    protected int mSelectorBottom = 0;
    protected int mSelectorSingle = 0;

    protected SettingItemOnClickListener mSettingItemClickListener;

    /** 在layout布局xml文件中创建时调用 */
    public AbstractSettingListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        mContext = context;
    }

    /** 列表项数目 */
    public abstract int getCount();

    /** 获取某一个位置的子view */
    public abstract View getView(int position, Context context);

    /** 获取某一个位置的子view所绑定的数据 */
    public abstract Object getItem(int position);

    /** 设置不同位置的子view背景的selector */
    public void setSelector(int upSelector, int midSelector,
        int bottomSelector, int singleSelector) {
        mSelectorUp = upSelector;
        mSelectorMid = midSelector;
        mSelectorBottom = bottomSelector;
        mSelectorSingle = singleSelector;

        showItems();
    }

    /** 加载子view并显示 */
    public void showItems() {
        this.removeAllViews();
        int count = getCount();
        for (int i = 0; i < count; i++) {
            this.addView(makeView(i, count));
        }
    }

    /** 构建子view */
    public View makeView(int position, int count) {
        View itemView = getView(position, mContext);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) itemView
            .getLayoutParams();
        if (position != 0) {
            params.setMargins(0, -1, 0, 0);
        }

        itemView.setLayoutParams(params);

        if (count > 1) {
            if (position == 0) {
                if (mSelectorUp != 0) {
                    itemView.setBackgroundResource(mSelectorUp);
                }
            } else if (position == count - 1) {
                if (mSelectorBottom != 0) {
                    itemView.setBackgroundResource(mSelectorBottom);
                }
            } else {
                if (mSelectorMid != 0) {
                    itemView.setBackgroundResource(mSelectorMid);
                }
            }
        } else if (mSelectorSingle != 0) {
            itemView.setBackgroundResource(mSelectorSingle);
        }

        itemView.setTag(position);
        itemView.setOnClickListener(this);

        return itemView;
    }

    @Override
    public void onClick(View v) {
        if (mSettingItemClickListener != null) {
            int position = (Integer) v.getTag();
            mSettingItemClickListener.onClick(v, position, getItem(position));
        }
    }

    public void setOnItemClickListener(SettingItemOnClickListener listener) {
        mSettingItemClickListener = listener;
    }

    public interface SettingItemOnClickListener {
        public void onClick(final View view, final int position,
            final Object data);
    }
}